/**
 * @PROJECT 
 * @DATE 2018年7月20日 下午8:33:14
 * @AUTHOR LUYU
 */
package com.kool.system.action.user;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kool.core.base.IBaseAction;
import com.kool.core.exception.AppException;
import com.kool.core.util.BeanUtils;
import com.kool.core.util.GlobalDefineUtils;
import com.kool.system.bean.SyUserInfoBean;
import com.kool.system.bean.io.SYUSRQRYZBean;
import com.kool.system.bean.io.user.SYUSRQRYXBean;
import com.kool.system.service.UserService;

/**
 * @DESCRIBE
 * @AUTHOR LUYU
 * @DATE 2018年7月20日 下午8:33:14
 *
 */
@Controller
public class QueryUserAction implements IBaseAction {
	@Autowired
	private UserService service;

	/**
	 * 
	 * @DESCRIBE
	 * @DATE 2018年7月8日 下午2:22:44
	 *
	 * @param sInput
	 * @return
	 * @throws AppException
	 */
	@ResponseBody
	@RequestMapping(value = "/sys/user/queryUser", method = RequestMethod.POST)
	public JSONObject doAction(@RequestBody String sInput) throws AppException {
		JSONObject pkgIn = JSONObject.parseObject(sInput);
		JSONArray sectionInUserInfo = pkgIn.getJSONArray("SYUSRQRYX");
		JSONObject propInUserInfo = sectionInUserInfo.getJSONObject(0);
		SYUSRQRYXBean ioUser = JSON.toJavaObject(propInUserInfo, SYUSRQRYXBean.class);
		SyUserInfoBean userInfo = null;
		if (ioUser != null) {
			userInfo = (SyUserInfoBean) BeanUtils.io2Bean(ioUser, SyUserInfoBean.class, "sui");
		} else {
			userInfo = new SyUserInfoBean();
		}

		List<SyUserInfoBean> list = service.query(userInfo);
		JSONObject pkgOut = new JSONObject();
		JSONArray sectionOutUsers = new JSONArray();
		for (SyUserInfoBean user : list) {
			SYUSRQRYZBean ioOut = (SYUSRQRYZBean) BeanUtils.bean2Io(user, SYUSRQRYZBean.class);
			JSONObject propOut = (JSONObject) JSON.toJSON(ioOut);
			GlobalDefineUtils.addTransformation(propOut, "type", "SUI_TYPE");
			GlobalDefineUtils.addTransformation(propOut, "hasKey", "SUI_HAS_KEY");
			GlobalDefineUtils.addTransformation(propOut, "state", "SUI_STATE");
			sectionOutUsers.add(propOut);
		}
		pkgOut.put("SYUSRQRYZ", sectionOutUsers);

		return pkgOut;
	}

}
